{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "import sys\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "import numpy as np\n",
    "import random\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入数据\n",
    "train=pd.read_csv(\"../data/train_set.csv\")\n",
    "test=pd.read_csv(\"../data/test_set.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LabelEncoder()"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result=test[['id']].copy()\n",
    "train_label=train['class'].values\n",
    "lb = LabelEncoder()\n",
    "lb.fit(train['class'].values)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#倒入概率\n",
    "# train test的概率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "path=\"./\"\n",
    "#\n",
    "capsule_lstm_train_char=np.load(path+\"capsule_lstm10_article.npz\")['train']\n",
    "#\n",
    "capsule_lstm_test_char=np.load(path+\"capsule_lstm10_article.npz\")['test']\n",
    "\n",
    "#词\n",
    "capsule_lstm_train_word=np.load(path+\"capsule_lstm10_word_seg.npz\")['train']\n",
    "#词\n",
    "capsule_lstm_test_word=np.load(path+\"capsule_lstm10_word_seg.npz\")['test']\n",
    "\n",
    "gru3_train_word=np.load(path+\"get_text_gru310_word_seg.npz\")['train']\n",
    "gru3_test_word=np.load(path+\"get_text_gru310_word_seg.npz\")['test']\n",
    "\n",
    "gru4_train_word=np.load(path+\"get_text_gru410_word_seg.npz\")['train']\n",
    "gru4_test_word=np.load(path+\"get_text_gru410_word_seg.npz\")['test']\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(102277, 19)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "capsule_lstm_train_word.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train=np.concatenate([capsule_lstm_train_char,\n",
    "                           capsule_lstm_train_word,\n",
    "                           gru3_train_word,\n",
    "                           gru4_train_word\n",
    "                       ],axis=1)\n",
    "\n",
    "x_test=np.concatenate([capsule_lstm_test_char,\n",
    "                           capsule_lstm_test_word,\n",
    "                           gru3_test_word,\n",
    "                           gru4_test_word\n",
    "                         ],axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(102277, 76)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import KFold\n",
    "kf = KFold(n_splits=5, shuffle=True, random_state=520).split(train['id'])\n",
    "fold_index=train[['id']].copy()\n",
    "for i, (train_fold, test_fold) in enumerate(kf):\n",
    "    fold_index.loc[test_fold,'fold']=int(i)\n",
    "fold_index['fold']=fold_index['fold'].astype(int)\n",
    "fold_index.to_csv('fold_index.csv',index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "nb_classes =19\n",
    "dims = x_train.shape[1]\n",
    "epochs = 15\n",
    "# parameter grids\n",
    "param_grid = [\n",
    "     #(1, 6, 0.73, 0.756, 0.00001, 0.017, 2400),\n",
    "     # (1, 8, 0.789, 0.97, 0, 0.018, 1100),\n",
    "     #(1, 5, 0.7, 0.7, 0.001, 0.01, 1500),\n",
    "     # (1, 6, 0.89, 0.994, 0.0001, 0.02421, 700),\n",
    "     #(1, 10, 0.74, 0.908, 0.0005, 0.0141, 1750),\n",
    "     #(1, 15, 0.7890, 0.890643, 0.231, 0.21, 900),\n",
    "     #(1, 19, 0.78, 0.97453, 0.00009, 0.01, 3900),\n",
    "     #(1, 6, 0.71, 0.71, 0, 0.01, 1250),\n",
    "      #(1, 8, 0.77, 0.83, 0.001, 0.03, 900),\n",
    "     (1, 3, 0.7, 0.7, 0.00008, 0.01, 300),\n",
    "     #(1, 8, 0.824, 0.0241, 0.000177,0.02406 ,743)        \n",
    "\n",
    "# kb8:  (1, 10, 0.87, 0.88, 0.000429, 0.029963, 652)        \n",
    "# kb9:  (1, 8, 0.824, 0.0241, 0.000177,0.02406 ,743 )         \n",
    "\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "from sklearn.metrics import f1_score\n",
    "import xgboost as xgb\n",
    "xfolds = pd.read_csv('fold_index.csv')\n",
    "# work with 5-fold split\n",
    "\n",
    "fold_index = xfolds.fold\n",
    "n_folds = len(np.unique(fold_index))\n",
    "train_model_pred = np.zeros((x_train.shape[0], 19))\n",
    "test_model_pred = np.zeros((x_test.shape[0], 19))\n",
    "for i in range(len(param_grid)):\n",
    "    print(\"processing parameter combo:\", param_grid[i])\n",
    "    # configure model with j-th combo of parameters\n",
    "    x = param_grid[i]\n",
    "    clf = xgb.XGBClassifier(objective='multi:softmax',\n",
    "                            n_estimators=x[6],\n",
    "                            max_depth=x[1],\n",
    "                            min_child_weight=x[0],\n",
    "                            learning_rate=x[5],\n",
    "                            silent=True,\n",
    "                            subsample=x[3],\n",
    "                            colsample_bytree=x[2],\n",
    "                            gamma=x[2],\n",
    "                            seed=6666,\n",
    "                            num_class=19,\n",
    "                            n_jobs=10)\n",
    "    for j in range(0,n_folds):\n",
    "        idx0 = np.where(fold_index != j)\n",
    "        idx1 = np.where(fold_index == j)  \n",
    "        x0 = np.array(x_train)[idx0,:][0]\n",
    "        x1 = np.array(x_train)[idx1,:][0]\n",
    "        y0 = np.array(train_label)[idx0]\n",
    "        y1 = np.array(train_label)[idx1]\n",
    "        clf.fit(x0, y0, eval_metric=\"mlogloss\", eval_set=[(x0, y0),(x1, y1)],verbose=100)\n",
    "\n",
    "        train_model_pred[idx1, :] =  clf.predict_proba(x1)\n",
    "        test_model_pred +=clf.predict_proba(x_test)\n",
    "        print (\"valid's macro-f1: %s\" % f1_score(y1.reshape(-1,1), \n",
    "                                                lb.inverse_transform(np.argmax( clf.predict_proba(x1), 1)).reshape(-1,1),\n",
    "                                                              average='macro'))\n",
    "\n",
    "        print(\"finished fold:\", j)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "print (\"offline test score: %s\" % f1_score(train_label.reshape(-1,1), \n",
    "                                      lb.inverse_transform(np.argmax(train_model_pred, 1)).reshape(-1,1),\n",
    "                                      average='micro'))\n",
    "\n",
    "clf.fit(x_train, train_label, eval_metric=\"mlogloss\",verbose=100)\n",
    "test_model_pred =clf.predict_proba(x_test)\n",
    "\n",
    "\n",
    "# In[14]:\n",
    "\n",
    "\n",
    "np.savez('stacking_offline8130664763338775.npz', train=train_model_pred, test=test_model_pred)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
